function FetchWeather()
{
	var zipCode = cityData[$("#SELECT_citySelector").val()].zip;
	$.ajax({
		  type: "GET",
		  dataType: 'json',
		  url: "/city/api/weather/"+zipCode
		}).done(function( msg ) {
			$("#FONT_currentTemp").html(msg.current.temp);
			$("#FONT_currentWeather").html(msg.current.text);
			$("#FONT_reportTime").html(msg.current.dateTimeDescription);
			$("#FONT_todayLowHigh").html(msg.today.low+'/'+msg.today.high);
			$("#FONT_todayWeather").html(msg.today.text);
			$("#FONT_tomorrowLowHigh").html(msg.tomorrow.low+'/'+msg.tomorrow.high);
			$("#FONT_tomorrowWeather").html(msg.tomorrow.text);
		}).fail(function( msg ) {
			$("#SELECT_citySelector")[0].selectedIndex=-1;
			$(".fieldValue").html("");
		});	
}

function FetchGeoInfo()
{
	var name = cityData[$("#SELECT_citySelector").val()].name;
	var yql='select * from geo.places.parent where child_woeid in (select woeid from geo.places where text="'+name+'")';
	$.ajax({
		  type: "GET",
		  dataType: 'json',
		  url: "/city/api/yql/"+encodeURI(yql)
		}).done(function( msg ) {
			var res=msg.query.results.place;
			var latLong=res.centroid.latitude+','+res.centroid.longitude;
			$("#FONT_country").html(res.country.content);
			$("#FONT_longitude").html(res.centroid.longitude);
			$("#FONT_lattitude").html(res.centroid.latitude);
			$("#SPAN_cityMap").html('<IMG id="IMG_citymap" src="http://maps.googleapis.com/maps/api/staticmap?center='+latLong+'&zoom=9&size=200x200&maptype=roadmap&sensor=false">');
		}).fail(function( msg, status, errorThrown ) {
			$("#SELECT_citySelector")[0].selectedIndex=-1;
			$(".fieldValue").html("");
		});	
}

function FetchYQLList(type, leftFieldName, rightFieldName)
{
	var name = cityData[$("#SELECT_citySelector").val()].name;
	var yql='select * from local.search where query="'+type+'" and location="'+name+'"';
	$.ajax({
		  type: "GET",
		  dataType: 'json',
		  url: "/city/api/yql/"+encodeURI(yql)
		}).done(function( msg ) {
			var res=msg.query.results.Result;
			var leftList='';
			var rightList='';
			$.each(res,function(index, item){
				if(index < 12)
				{
					if(item.Url)
						rightList+='<a target=RestaurantPage href="'+item.Url+'">';
					rightList+=item.Title;
					if(item.Url)
						rightList+='</a>';
					rightList+='<br>';
					if(item.Categories.Category)
					{
						if(item.Categories.Category[0])
							leftList+=item.Categories.Category[0].content;
						else
							leftList+=item.Categories.Category.content;
					}
					leftList+='<br>';
				}
			});
			if(leftFieldName)
				$(leftFieldName).html(leftList);
			if(rightFieldName)
				$(rightFieldName).html(rightList);
		}).fail(function( msg, status, errorThrown ) {
		});	
}

function FetchGeoNearby()
{
	var name = cityData[$("#SELECT_citySelector").val()].name;
	var yql='select * from geo.places.siblings where sibling_woeid in (select woeid from geo.places where text="'+name+'" limit 1)';
	$.ajax({
		  type: "GET",
		  dataType: 'json',
		  url: "/city/api/yql/"+encodeURI(yql)
		}).done(function( msg ) {
			var res=msg.query.results.place;
			var placesNearby='';
			$.each(res,function(index, item){
				if(index < 8)
				{
					if(placesNearby!='')
						placesNearby+='<br>';
					placesNearby+=item.name+' ('+item.placeTypeName.content+')';
				}
			});
			$("#FONT_nearby").html(placesNearby);
		}).fail(function( msg, status, errorThrown ) {
			$("#SELECT_citySelector")[0].selectedIndex=-1;
			$(".fieldValue").html("");
		});	
}

var ChangeCities = function()
{
	$(".fieldValue").html("");
	FetchWeather();
	FetchGeoInfo();
	FetchGeoNearby();
	FetchYQLList('restaurant','#FONT_restaurantsLeftColumn','#FONT_restaurantsRightColumn');
	FetchYQLList('hotel','#FONT_hotelsLeftColumn','#FONT_hotelsRightColumn');
};
 
$(document).ready(function(){
	$("#SELECT_citySelector").change(ChangeCities);
	$.each(cityData, function(key, value) {   
	     $('#SELECT_citySelector')
	          .append($('<option>', { value : key })
	          .text(value.display));
	});
	$("#SELECT_citySelector")[0].selectedIndex=-1;
});
